package com.jd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jd.pojo.Book;
import com.jd.pojo.Evaluation;
import com.jd.pojo.Member;
import com.jd.pojo.MemberReadState;
import com.jd.service.BookService;
import com.jd.service.EvaluationService;
import com.jd.service.MemberReadStateService;
import com.jd.service.MemberService;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private MemberReadStateService memberReadStateService;

    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Book> findPage(Long categoryId,String order,Integer p){
        IPage<Book> paging = bookService.Paging(categoryId,order,p, 10);
        return paging;
    }

    @RequestMapping("/books/{id}")
    public ModelAndView showDetile(@PathVariable("id") Long bookId, HttpServletRequest request){
        /*从session中获取会员信息*/
        Member member = (Member) request.getSession().getAttribute("checkLogin");

        ModelAndView modelAndView=new ModelAndView("/detail");

        Book book = bookService.findBookById(bookId);
        List<Evaluation> evaluation = evaluationService.findEvaluation(bookId);

        //用户已经登录
        if(member!=null){
            MemberReadState memberReadState = memberReadStateService.selectState(member.getMemberId(), bookId);
            modelAndView.addObject("memberReadState",memberReadState);
        }

        modelAndView.addObject("book",book);
        modelAndView.addObject("evaluation",evaluation);

        return modelAndView;
    }
}
